package com.swust.book_shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swust.book_shop.mapper.UserInfoMapper;
import com.swust.book_shop.pojo.BookInfo;
import com.swust.book_shop.pojo.UserInfo;
import com.swust.book_shop.pojo.vo.BookItemVo;
import com.swust.book_shop.pojo.vo.BookQueryVo;
import com.swust.book_shop.pojo.vo.BookVo;
import com.swust.book_shop.service.BookInfoService;
import com.swust.book_shop.mapper.BookInfoMapper;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

import static com.swust.book_shop.constant.ServiceConstant.ENABLE_UP_BOOK;

/**
* @author 86151
* @description 针对表【book_info】的数据库操作Service实现
* @createDate 2022-05-04 23:16:39
*/
@Service
@Slf4j
public class BookInfoServiceImpl extends ServiceImpl<BookInfoMapper, BookInfo> implements BookInfoService{
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    BookInfoMapper bookInfoMapper;
    @Override
    public int uploadBook(String account, BookVo bookVo) {
        QueryWrapper<UserInfo> queryWrapperUser =new QueryWrapper<>();
        QueryWrapper<BookInfo> bookInfoQueryWrapper = new QueryWrapper<>();
        UserInfo user = userInfoMapper.selectOne(queryWrapperUser.eq("account", account));
        if(ENABLE_UP_BOOK.equals(user.getIsUpBook())){
            BookInfo upBook = new BookInfo();
            BeanUtils.copyProperties(bookVo,upBook);
            //设置上传书本人的id
            upBook.setPublisherId(user.getId());
            bookInfoQueryWrapper.eq("book_isbn",bookVo.getBookIsbn());
            //判断是否有该书
            BookInfo isHaveBook = bookInfoMapper.selectOne(bookInfoQueryWrapper);
            if(isHaveBook == null){
                upBook.setStock(bookVo.getUpNum());
                int insert = bookInfoMapper.insert(upBook);
                log.info("插入了{}条记录",insert);
                Integer id =  upBook.getId();
                return id;
            }else {
                //更新库存
                Integer stock = isHaveBook.getStock();
                stock += bookVo.getUpNum();
                isHaveBook.setStock(stock);
                int i = bookInfoMapper.updateById(isHaveBook);
                log.info("更新了{}条记录",i);
                return isHaveBook.getId();
            }
        }else {
            throw  new RuntimeException("权限不足,请实名认证");
        }
    }

    @Override
    public IPage<BookItemVo> queryBookItemLimit(IPage iPage) {
        IPage iPage1 = bookInfoMapper.queryBookItemLimit(iPage);
        return iPage1;
    }

    @Override
    public IPage<BookItemVo> queryBookItemLimitByPriceOrBookName(IPage iPage,BookQueryVo bookQueryVo) {
        BigDecimal bigDecimal = Optional.ofNullable(bookQueryVo.getLow()).orElse(new BigDecimal("0"));
        BigDecimal bigDecimal1 = Optional.ofNullable(bookQueryVo.getHigh()).orElse(new BigDecimal("0"));
        bookQueryVo.setLow(bigDecimal);
        bookQueryVo.setHigh(bigDecimal1);
        IPage iPage1 = bookInfoMapper.queryBookItemLimitByPriceOrName(iPage, bookQueryVo.getBookName(), bookQueryVo.getLow(), bookQueryVo.getHigh());
        return iPage1;
    }

    @Override
    public IPage<BookItemVo> queryBookByCategory(IPage iPage, String category) {
        IPage iPage1 = bookInfoMapper.queryBookByCategory(iPage, category);
        return iPage1;
    }

}




